package com.bywlkjs.bygo.wechat.applet.spring.boot.session;

import com.bywlkjs.common.util.CookieUtil;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;


/**
 * 由于小程序的session不能直接再HttpSession中获取，
 * 所以自己编写了session
 */
@Component
public class AppletSession implements HandlerInterceptor, WebMvcConfigurer {
    public static final String SESSION_NAME = "APPLET_SESSION";
    private static ExpiringMap<String, HashMap<String, Object>> sessionMap;

    public void setAttribute(String key, Object value,HttpServletRequest request){
        sessionMap.get(new CookieUtil(request.getCookies()).get(AppletSession.SESSION_NAME)).put(key,value);
    }

    public Object getAttribute(String key,HttpServletRequest request){
        return sessionMap.get(new CookieUtil(request.getCookies()).get(AppletSession.SESSION_NAME)).get(key);
    }

    public void setAttribute(String key, Object value,String sessionId){
        sessionMap.get(sessionId).put(key,value);
    }

    public Object getAttribute(String key,String sessionId){
        return sessionMap.get(sessionId).get(key);
    }

    public AppletSession(){
        sessionMap=
                ExpiringMap.builder()
                .expiration(1800000, TimeUnit.MILLISECONDS) //设置三十分钟的定时
                .expirationPolicy(ExpirationPolicy.ACCESSED)
                .variableExpiration()
                .build();
    }

    public String getId(HttpServletRequest request){
        CookieUtil cookieUtil = new CookieUtil(request.getCookies());
        return cookieUtil.get(AppletSession.SESSION_NAME);
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        CookieUtil cookieUtil = new CookieUtil(request.getCookies());
        String sessionId = cookieUtil.get(AppletSession.SESSION_NAME);
        if (sessionMap.get(sessionId) == null) {
            sessionMap.put(sessionId, new HashMap<>());
            return true;
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AppletSession()).excludePathPatterns("**");
    }

}
